package com.sqlcheck.repository;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sqlcheck.entity.ScanTask;
import com.sqlcheck.enums.TaskStatus;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;

@Mapper
public interface ScanTaskRepository extends BaseMapper<ScanTask> {

    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC"
    })
    List<ScanTask> findByUserId(@Param("userId") Long userId);

    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE project_id = #{projectId} AND deleted = 0 ORDER BY created_at DESC"
    })
    List<ScanTask> findByProjectId(@Param("projectId") Long projectId);

    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE status = #{status} AND deleted = 0"
    })
    List<ScanTask> findByStatus(@Param("status") TaskStatus status);

    @Update("UPDATE scan_tasks SET status = #{status}, progress = #{progress}, updated_at = NOW() WHERE id = #{id}")
    int updateStatusAndProgress(@Param("id") Long id, @Param("status") TaskStatus status,
            @Param("progress") Integer progress);

    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE user_id = #{userId} AND deleted = 0 ORDER BY created_at DESC"
    })
    IPage<ScanTask> findByUserIdWithPage(Page<ScanTask> page, @Param("userId") Long userId);

    // 管理员查看所有任务的方法
    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE deleted = 0 ORDER BY created_at DESC"
    })
    List<ScanTask> findAllTasks();

    @Select({
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE deleted = 0 ORDER BY created_at DESC"
    })
    IPage<ScanTask> findAllTasksWithPage(Page<ScanTask> page);

    // 根据项目查找任务，支持数据权限过滤
    @Select({
        "<script>",
        "SELECT id, task_name, task_type, status, progress, project_id, branch_name, user_id, description,",
        "config AS configJson, source_db_type, target_db_type, ai_enhancement_enabled, ai_analysis_status,",
        "ai_filter_config AS aiFilterConfigJson, rule_analysis_completed_at, ai_analysis_completed_at,",
        "problem_sql_count, ai_processed_count, extraction_stats AS extractionStatsJson, start_time, end_time,",
        "error_message, report_path, retry_count, max_retry_count, last_retry_time,",
        "ai_analysis_start_time, ai_analysis_end_time, ai_analysis_duration, ai_processed_sql_count, ai_skip_reason,",
        "created_at, updated_at, deleted",
        "FROM scan_tasks WHERE project_id = #{projectId} AND deleted = 0",
        "<if test='filterUserId != null'>",
        "AND user_id = #{filterUserId}",
        "</if>",
        "ORDER BY created_at DESC",
        "</script>"
    })
    List<ScanTask> findByProjectIdWithPermission(@Param("projectId") Long projectId, @Param("filterUserId") Long filterUserId);
}